﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.TimKiem
{
    public partial class frmHoaDonNhap : Form
    {
        bool checkClose = false;
        public static Entities.HoaDonNhap hdbanbuon;
        Entities.HoaDonNhap[] hienthi;

        public frmHoaDonNhap()
        {
            InitializeComponent();
        }

        public frmHoaDonNhap(string makh, bool loaiphieu)
        {
            InitializeComponent();
            if (makh != "" && loaiphieu == true)
            {
                hienthi = HienThiTheoMaNCC(makh);
                dgvHienThi.DataSource = hienthi;
                dgvHienThi.Focus();
            }
            if (makh != "" && loaiphieu == false)
            {
                hienthi = HienThiHDTraLaiNCCTheoMaNCC(makh);
                dgvHienThi.DataSource = hienthi;
                dgvHienThi.Focus();
            }
            fix();

        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.Columns["MaHoaDonNhap"].HeaderText = "Mã Hóa Đơn Nhập";
            dgvHienThi.Columns["MaHoaDonNhap"].Visible = true;
            dgvHienThi.Columns["TongTien"].HeaderText = "Tổng Tiền";
            dgvHienThi.Columns["TongTien"].Visible = true;
            dgvHienThi.Columns["ThanhToanNgay"].HeaderText = "Thanh Toán Ngay";
            dgvHienThi.Columns["ThanhToanNgay"].Visible = true;
            dgvHienThi.Columns["ThanhToanSauKhiLapPhieu"].HeaderText = "Thanh Toán Sau Khi Lập Phiếu";
            dgvHienThi.Columns["ThanhToanSauKhiLapPhieu"].Visible = true;
            if (dgvHienThi.RowCount > 0)
            {
                Common.Utilities.CountDatagridview(dgvHienThi);
                dgvHienThi.Focus();
                dgvHienThi.Select();
            }
            dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


        }
        public Entities.HoaDonNhap[] HienThiTheoMaNCC(string mancc)
        {
            Entities.HoaDonNhap[] hoadonnhap = Common.StaticVariable.InterfaceObject.SelectHDNhapTheoMaNCC(mancc);
            
            Entities.HoaDonNhap[] temp = new Entities.HoaDonNhap[hoadonnhap.Length];
            int sl = 0;
            for (int i = 0; i < hoadonnhap.Length; i++)
            {
                if (hoadonnhap[i].Deleted == false)
                {
                    double ttn = Convert.ToDouble(hoadonnhap[i].ThanhToanNgay);
                    double ttnklp = Convert.ToDouble(hoadonnhap[i].ThanhToanSauKhiLapPhieu);
                    double tt = Convert.ToDouble(hoadonnhap[i].TongTien);
                    if ((ttn + ttnklp) < tt)
                    {
                        temp[sl] = hoadonnhap[i];
                        sl++;
                    }
                }
            }
            hoadonnhap = new Entities.HoaDonNhap[sl];
            for (int i = 0; i < hoadonnhap.Length; i++)
            {
                hoadonnhap[i] = temp[i];
            }
            //lbTongSo.Content = Common.Constants.TongSo + hoadonnhap.Length;
            return hoadonnhap;
        }

        public Entities.HoaDonNhap[] HienThiHDTraLaiNCCTheoMaNCC(string mancc)
        {
            Entities.TraLaiNCC[] hoadonnhap = Common.StaticVariable.InterfaceObject.SelectHDTraLaiNCCTheoMaNCC(mancc);

            Entities.TraLaiNCC[] temp = new Entities.TraLaiNCC[hoadonnhap.Length];
            int sl = 0;
            for (int i = 0; i < hoadonnhap.Length; i++)
            {
                if (hoadonnhap[i].Deleted == false)
                {
                    double ttn = Convert.ToDouble(hoadonnhap[i].ThanhToanNgay);
                    double ttnklp = Convert.ToDouble(hoadonnhap[i].ThanhToanSauKhiLapPhieu);
                    double tt = Convert.ToDouble(hoadonnhap[i].TienBoiThuong);
                    if ((ttn + ttnklp) < tt)
                    {
                        temp[sl] = hoadonnhap[i];
                        sl++;
                    }
                }
            }
            hoadonnhap = new Entities.TraLaiNCC[sl];
            for (int i = 0; i < hoadonnhap.Length; i++)
            {
                hoadonnhap[i] = temp[i];
            }
            ArrayList array = new ArrayList();
            foreach (Entities.TraLaiNCC item in temp)
            {
                Entities.HoaDonNhap hdnhap = new Entities.HoaDonNhap();
                hdnhap.MaHoaDonNhap = item.MaHDTraLaiNCC;
                hdnhap.ThanhToanNgay = item.ThanhToanNgay;
                hdnhap.ThanhToanSauKhiLapPhieu = item.ThanhToanSauKhiLapPhieu;
                hdnhap.TongTien = item.TienBoiThuong;
                array.Add(hdnhap);
            }
            //lbTongSo.Content = Common.Constants.TongSo + hoadonnhap.Length;
            return (Entities.HoaDonNhap[])array.ToArray(typeof(Entities.HoaDonNhap));
        }

        #region Chọn
        void Chon()
        {
            try
            {
                if (hdbanbuon == null)
                {
                    MessageBox.Show("Bạn chưa chọn nhà cung cấp", "Hệ thống cảnh báo", MessageBoxButtons.OK);
                    return;
                }
                checkClose = true;
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) ==  DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        private void Window_Loaded(object sender, EventArgs e)
        {
            this.Closing += new System.ComponentModel.CancelEventHandler(frmTimKiemHDBanHang_Closing);
            // click chuột trái image
            btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
            btnChon.MouseDown += new MouseEventHandler(MouseDown_Chung);
            // text changed
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            // key up
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex; 
            if (i < 0)
                return;
            try
            {
                Chon();
            }
            catch
            {
            }
        }

        void frmTimKiemHDBanHang_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        int i = -1;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            for (int i1 = 0; i1 < hienthi.Length; i1++)
            {
                if (hienthi[i1].MaHoaDonNhap == dgvHienThi.CurrentRow.Cells["MaHoaDonNhap"].Value.ToString())
                {
                    hdbanbuon = hienthi[i1];
                }
            }
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            int soluong = 0;
            if (rdbTimKiem1.Checked == true)
            {
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].MaNhaCungCap.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        soluong++;
                    }
                }
                if (soluong == 0)
                {
                    dgvHienThi.DataSource = new Entities.HoaDonNhap[0];
                    //lbTongSo.Content = Common.Constants.TongSo + "0";
                    return;
                }
                Entities.HoaDonNhap[] hienthitheoid = new Entities.HoaDonNhap[soluong];
                soluong = 0;
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].MaNhaCungCap.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        hienthitheoid[soluong] = hienthi[i];
                        soluong++;
                    }
                }
                 dgvHienThi.DataSource = hienthitheoid;
                //lbTongSo.Content = Common.Constants.TongSo + hienthitheoid.Length;

            }
        }

        private void MouseDown_Chung(object sender, MouseEventArgs e)
        {
            try
            {
                Button temp = (Button)sender;

                #region chuột trái
                
                    switch (temp.Name)
                    {
                        case Common.Constants.btnChon:
                            {
                                Chon();
                                break;
                            }
                        case Common.Constants.btnThoat:
                            {
                                Thoat();
                                break;
                            }
                        default:
                            break;
                    }
                
                #endregion
            }
            catch
            {
            }
        }

        private void KeyDown_Chung(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F2)
                {
                    Chon();
                }
                if (e.KeyCode == Keys.Escape)
                {
                    Thoat();
                }
                if (e.KeyCode == Keys.Enter)
                {
                    Chon();
                }
            }
            catch
            {
            }
        }
    }
}
